﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Maticsoft.DBUtility;

namespace ZG_member.DAL
{
    public class Leave
    {
        //添加数据
        public bool add(ZG_member.Model.Leave model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Leave(");
            strSql.Append("memberId,StartDate,EndDate)");
            strSql.Append("values (");
            strSql.Append("@memberId,@StartDate,@EndDate)");
            SqlParameter[] parameters = {
					 
					new SqlParameter("@memberId", SqlDbType.VarChar,50),
					new SqlParameter("@StartDate", SqlDbType.DateTime),
					new SqlParameter("@EndDate", SqlDbType.DateTime)};

            parameters[0].Value = model.memberId;
            parameters[1].Value = model.StartDate;
            parameters[2].Value = model.EndDate;


            object obj = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);

            if (obj == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public DataSet GetListWhere(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select *");
            strSql.Append(" FROM Leave,memberInfo where Leave.memberId=memberInfo.memberId ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" and " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ZG_member.Model.Leave model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Leave set ");
            strSql.Append("memberId=@memberId,");
            strSql.Append("StartDate=@StartDate,");
            strSql.Append("EndDate=@EndDate");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@memberId", SqlDbType.VarChar,50),
					new SqlParameter("@StartDate", SqlDbType.DateTime),
					new SqlParameter("@EndDate", SqlDbType.DateTime),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.memberId;
            parameters[1].Value = model.StartDate;
            parameters[2].Value = model.EndDate;
            parameters[3].Value = model.Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return DbHelperSQL.Query(strWhere.ToString());
        }


        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Leave ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        public ZG_member.Model.Leave GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,memberId,StartDate,EndDate from Leave ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            ZG_member.Model.Leave model = new ZG_member.Model.Leave();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["memberId"] != null && ds.Tables[0].Rows[0]["memberId"].ToString() != "")
                {
                    model.memberId = ds.Tables[0].Rows[0]["memberId"].ToString();
                }
                if (ds.Tables[0].Rows[0]["StartDate"] != null && ds.Tables[0].Rows[0]["StartDate"].ToString() != "")
                {
                    model.StartDate = Convert.ToDateTime(ds.Tables[0].Rows[0]["StartDate"]);
                }
                if (ds.Tables[0].Rows[0]["EndDate"] != null && ds.Tables[0].Rows[0]["EndDate"].ToString() != "")
                {
                    model.EndDate = Convert.ToDateTime(ds.Tables[0].Rows[0]["EndDate"]);
                }
               
                return model;
            }
            else
            {
                return null;
            }
        }

        public ZG_member.Model.Leave GetModel(string memberId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 Id,memberId,StartDate,EndDate from Leave ");
            strSql.Append(" where memberId=@memberId");
            SqlParameter[] parameters = {
					new SqlParameter("@memberId", SqlDbType.VarChar,20)
};
            parameters[0].Value = memberId;

            ZG_member.Model.Leave model = new ZG_member.Model.Leave();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["memberId"] != null && ds.Tables[0].Rows[0]["memberId"].ToString() != "")
                {
                    model.memberId = ds.Tables[0].Rows[0]["memberId"].ToString();
                }
                if (ds.Tables[0].Rows[0]["StartDate"] != null && ds.Tables[0].Rows[0]["StartDate"].ToString() != "")
                {
                    model.StartDate = Convert.ToDateTime(ds.Tables[0].Rows[0]["StartDate"]);
                }
                if (ds.Tables[0].Rows[0]["EndDate"] != null && ds.Tables[0].Rows[0]["EndDate"].ToString() != "")
                {
                    model.EndDate = Convert.ToDateTime(ds.Tables[0].Rows[0]["EndDate"]);
                }
               
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="Size">一页多少个</param>
        /// <param name="Num">当前第几页</param>
        /// <returns></returns>
        public DataSet GetList(int Size, int Num,string where)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select top("+Size+") Leave.Id,Leave.memberId,memberInfo.memberName,StartDate,EndDate");
            if (where == "")
            {
                sb.Append(" FROM Leave,memberInfo where Leave.Id not in (select  top((" + Num + "-1)*" + Size + ") Leave.Id from Leave,memberInfo where Leave.memberId=memberInfo.memberId  order by id)");
                sb.Append("  and  Leave.memberId=memberInfo.memberId");
            }
            else
            {
                sb.Append(" FROM Leave,memberInfo where Leave.Id not in (select  top((" + Num + "-1)*" + Size + ") Leave.Id from Leave,memberInfo where Leave.memberId=memberInfo.memberId and "+where+" order by id)");
                sb.Append("  and  Leave.memberId=memberInfo.memberId and "+where+"");
            }
            return DbHelperSQL.Query(sb.ToString());
        }


    }
}
